const SFTPClient = require("ssh2-sftp-client");
const fs = require("fs");
const utils = require("../utils/index.js");

// 创建一个 SFTP 客户端实例
let remoteConfig = {
  host: "", // 远程服务器域名或IP地址
  port: "", // SSH端口，默认是22
  username: "root", // SSH登录用户名
  password: "", // SSH登录密码
  // 压缩选项
  algorithms: {
    compress: ["zlib@openssh.com", "zlib", "none"],
  },
};
module.exports = {
  serverInfo(type, localPath, fileName) {
    console.log("进", type, "-type", fileName, "-fileName");
    console.log(utils.formatDate(new Date()),'-----------开始上传时间')

    let dot = utils.getSubstringAfterLastDot(fileName);
    return new Promise((resolve, reject) => {
      let path = "";
      const sftp = new SFTPClient();
      if (type == "typeAI") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveIosTypeAi-node/${fileName}`;
      } else if (type == "android-ins") {
        //ins2
        remoteConfig.host = "170.187.230.149";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-149";
        path = `/saveAndroidFile-node/${fileName}`;
      } else if (type == "android-tt") {
        remoteConfig.host = "170.187.230.149";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-149";
        path = `/saveAndroidFile-node/${fileName}`;
      } else if (type == "android-all") {
        remoteConfig.host = "170.187.230.149";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-149";
        path = `/saveAndroidAllFile-node/${fileName}`;
      } else if (type == "android-ins3" && dot == "apk") {
        remoteConfig.host = "170.187.230.149";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-149";
        path = `/saveAndroidIns3-node/${fileName}`;
      } else if (type == "android-ins3" && dot == "aab") {
        remoteConfig.host = "172.104.35.128";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-128";
        path = `/saveAndroidIns3-node/${fileName}`;
      } else if (type == "android-ins1") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveAndroidIns1-node/${fileName}`;
      } else if (type == "ios-tt") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveFile-node/${fileName}`;
      } else if (type == "ios-ins") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveFile-node/${fileName}`;
      } else if (type == "pdf") {
        remoteConfig.host = "172.104.35.128";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-128";
        path = `/savePdfAndroidFile-node/${fileName}`;
      } else if (type == "remoteTV") {
        remoteConfig.host = "172.104.35.128";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-128";
        path = `/saveRemoteTV-node/${fileName}`;
      } else if (type == "wamr") {
        remoteConfig.host = "172.104.35.128";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-128";
        path = `/saveWamrFile-node/${fileName}`;
      } else if (type == "stepCounter") {
        remoteConfig.host = "172.104.35.128";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-128";
        path = `/saveStepCounterFile-node/${fileName}`;
      } else if (type == "calculator") {
        remoteConfig.host = "192.46.226.198";
        remoteConfig.port = "22";
        remoteConfig.password = "zhijianNo.1-198";
        path = `/saveCalculatorLock-node/${fileName}`;
      } else if (type == "HeartRate") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveFile/${fileName}`;
      } else if (type == "roku") {
        remoteConfig.host = "45.76.212.42";
        remoteConfig.port = "56000";
        remoteConfig.password = "zhijianNo.1-42";
        path = `/saveIosRokuRemoteNode/${fileName}`;
      }
      sftp
        .connect(remoteConfig)
        .then(() => {
          const fileSize = fs.statSync(localPath).size;
          let transferred = 0;
          let startTime = Date.now();

          sftp.on("upload", (info) => {
            transferred += info.chunkLength;
            let percentComplete = ((transferred / fileSize) * 100).toFixed(2);
            let elapsedTime = (Date.now() - startTime) / 1000; // seconds
            let speed = (transferred / elapsedTime).toFixed(2);

            console.log(`Uploaded: ${percentComplete}%`);
            console.log(`Speed: ${speed} bytes/sec`);
          });
          return sftp.put(localPath, path);
        })
        .then(() => {
          console.log("文件上传成功！");
          console.log(utils.formatDate(new Date()),'-----------上传完成时间')

          resolve(utils.getDomainByIP(remoteConfig.host) + path);
        })
        .catch((err) => {
          console.error("文件上传失败：", err);
          reject(err);
        })
        .finally(() => {
          sftp.end();
        });
    });
  },
};
